﻿using System.Collections.Generic;
using System.Linq;
using Samples.Radio.Agent.Model;
using WindowsPhoneAudioStreaming.Audio.Streaming;

namespace Samples.Radio.Helpers
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Extension method to create a playlist from the list of podcasts
        /// </summary>
        /// <param name="podcasts"></param>
        /// <returns></returns>
        public static List<PlaylistTrack> ToPlaylist(this List<PodcastViewModel> podcasts)
        {
            return (from podcast in podcasts
                    where podcast != null
                    select podcast.ToPlaylistTrack()).ToList();
        }

        public static PlaylistTrack ToPlaylistTrack(this PodcastViewModel podcast)
        {
            return new PlaylistTrack
                {
                    Title = podcast.Title,
                    Source = podcast.PodcastUrl,
                    IsStreaming = false,
                    PlayerControls = Microsoft.Phone.BackgroundAudio.EnabledPlayerControls.All
                };
        }

        public static TrackViewModel ToTrackViewModel(this AudioStreamDefinition streamDefinition)
        {
            return new TrackViewModel()
                       {
                           TrackName = streamDefinition.Title,
                           AlbumArt = streamDefinition.ImageUri,
                           Album = streamDefinition.StreamUri
                       };
        }

        public static ProgramViewModel ToProgramViewModel(this PodcastViewModel podcast, ProgramViewModel program)
        {
            string imageUrl = podcast.ProgramImage;

            ///Use program image if the podcast doesn't have an image
            if(string.IsNullOrEmpty(imageUrl))
            {
                imageUrl = program.ProgramImage;
            }

            return new ProgramViewModel()
            {
                Description = podcast.Description,
                StartTime = podcast.Date,
                Keywords = podcast.Keywords,
                ProgramImage = imageUrl,
                ProgramName = podcast.Title
            };
        }

    }


}
